//
//  OGOutlineViewController.m
//  OsteoGest
//
//  Created by Pierre de Filippis on 10/7/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "OGOutlineViewController.h"


@implementation OGOutlineViewController

- (id)init
{
	if (self = [super init])
	{
		//rootItem = [[Node alloc] init];
//		[rootItem setItemName:@"Customers"];
		_customers = [[NSMutableArray alloc] init];
		NSLog(@"OutlineView controller initialized");
	}
	return self;
}

- (void) dealloc
{
	//[rootItem release];
	[_customers release];
	[super dealloc];
}

- (NSMutableArray *) customers
{
	NSLog(@"In customers");
	NSLog(@"Customers count: %d", [_customers count]);
	if ([_customers count] > 0)
		NSLog(@"%@", [[[_customers objectAtIndex:[_customers count]-1] properties] valueForKey:@"first"]);
	return _customers;
}

//- (void) setCustomers: (NSArray *)newCustomers
//{
//
//	NSLog(@"In setCustomers");
//	if (_customers != newCustomers)
//	{
//		[_customers autorelease];
//		_customers = [[NSMutableArray alloc] initWithArray:newCustomers];
//	}
//}

//- (void)addCustomer
//{
//	// Which row is selected?
//    int selectedRow = [_column selectedRow];
//    Node *selectedItem;
//    Node *newNode;
//    
//    // Is nothing selected?
//    if (selectedRow < 0)
//        selectedItem = [_column itemAtRow:0];
//	else
//		selectedItem  = [_column itemAtRow:selectedRow];
//    
//    // Create a new child node
//    newNode =  [[Node alloc] init];
//    [newNode setItemName:@"No Name"];
//    [selectedItem addChild: newNode];
//			
//    // Parent node is retaining the new node
//    [newNode release];
//
//	[_column reloadData];
//	if ([selectedItem isEqual:rootItem])
//		[_column expandItem:selectedItem];
//
//    // Brute force reload to update sums
//    // [outlineView reloadItem:rootNode reloadChildren:YES];
//}
//
//- (void)removeCustomer
//{
//	int selectedRow = [_column selectedRow];
//	Node *selectedItem;
//		
//	// Is nothing or root selected?
//	if (selectedRow < 1) {
//		NSBeep();
//		return;
//	}
//	
//	selectedItem = [_column itemAtRow:selectedRow];
//	[[selectedItem parent] removeChild:selectedItem];
//	
//	[_column reloadData];
//}

#pragma mark OUTLINE VIEW DATA SOURCE METHODS

- (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
    if (item == nil) {
        // The root object;
        return 1;
    }
    return [item childrenCount];
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
    return  [item expandable];
}

- (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
{
	if (item == nil)
		return rootItem;
	else
		return [item childAtIndex:index];
}

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
	if ([item isEqual: rootItem])
		return [[item itemName] uppercaseString];
	else
		return [item itemName];
}

#pragma mark OUTLINE VIEW DELEGATE METHODS

//- (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
//{
//	if ([item isEqual: rootItem])
//		return NO;
//	return YES;
//}
//
//- (NSIndexSet *)outlineView:(NSOutlineView *)outlineView selectionIndexesForProposedSelection:(NSIndexSet *)proposedSelectionIndexes
//{
//	// If we are trying to select the root
//	if ([proposedSelectionIndexes containsIndex:0])
//	{
//		NSMutableIndexSet *returnSet = [[NSMutableIndexSet alloc] initWithIndexSet:proposedSelectionIndexes];
//		[returnSet removeIndex:0];
//		return [returnSet autorelease];
//	}
//	return proposedSelectionIndexes;
//}
//
//- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item;
//{
//	if ([item isEqual:rootItem])
//		return YES;
//	else
//		return NO;
//}

@end
